﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hotnet.Dal.NSDalNews;
using Hotnet.Entity;
using Ninject.Core;
using Hotnet.Biz.NSBizCommon;
using Hotnet.Entity.Enum;

namespace Hotnet.Biz.NSBizNews
{
	public class BizNewsXTag : BaseBiz<NewsXTag>
	{
		private static DalNewsXTag db = new DalNewsXTag();
		private BizTag bizTag;
		private BizTag BizTag
		{
			get
			{
				if (bizTag == null)
					bizTag = new BizTag();
				return bizTag;
			}
		}
		public BizNewsXTag() : base(db) { }
		public void DeleteAllByNewsId(int newsId)
		{
			db.DeleteBy(newsXTag => newsXTag.NewsID == newsId);
		}

		public void SaveTags(string tags, int newsId)
		{
			var tagIDs = BizTag.Save(tags, EnumTagType.新闻);

			foreach (var tagId in tagIDs)
			{
				var newsXTag = new NewsXTag();
				newsXTag.NewsID = newsId;
				newsXTag.TagID = tagId;
				this.Insert(newsXTag);
			}
		}

		//public NewsXTag[] ListByNewsId(int newsId)
		//{
		//    var tags = db.ListBy(news => news.NewsID == newsId);
		//    foreach (var item in tags)
		//    {
		//        item.Tag = BizTag.Get(item.TagID);
		//    }
		//    return tags;
		//}

		public string GetTagStringsByNewsId(int newsId)
		{
			NewsXTag[] newsXTags = db.ListBy(news => news.NewsID == newsId);
			StringBuilder sb = new StringBuilder();
			foreach (var newsXTag in newsXTags)
			{
				sb.Append(BizTag.Get(newsXTag.TagID).TagName).Append(" ");
			}
			return sb.ToString().Trim();

		}

		public IList<string> ListTagNamesByNewsID(int newsId)
		{
			NewsXTag[] newsXTags = db.ListBy(news => news.NewsID == newsId);
			IList<string> newsIdList = new List<string>();
			foreach (var newsTag in newsXTags)
			{
				newsIdList.Add(BizTag.Get(newsTag.TagID).TagName);
			}
			return newsIdList;
		}
	}
}
